const {
  $util,
} = getApp();

Page({
  data: {
    showPaper: true, // false 显示考试说明 true显示试卷
    disabled: true,
  },
  onLoad(opts) {
    this.setData({
      id: opts.id,
    });
    this.loadDetail(opts.id);
    $util.request({
      url: 'entry/wxapp/partylife.talk.getdetails',
      data: {
        id: opts.id,
      },
      success: (res) => {
        this.setData(res.data);
      },
    });
  },
  onShow() {
    let wxData = wx.getStorageSync('qyUserInfo') || {};
    this.setData(wxData);
    this.setData({
      isSaaS: wx.getStorageSync('isSaaS'),
    })
  },
  startShowPaper() {
    this.setData({
      showPaper: true,
    });
  },
  loadDetail(reviewid) {
    $util.request({
      url: 'entry/wxapp/partylife.review.getdetails',
      data: {
        reviewid,
      },
      success: (res) => {
        this.setData({
          list: res.data,
          is_discussion: res.is_discussion,
          isLoaded: true,
          dType: res.detail.type,
          discuss_target: res.detail.discuss_target,
          desc: res.detail.desc,
          targetUserid: res.detail.userid,
        });
        // res.detail.title && wx.setNavigationBarTitle({
        //   title: res.detail.title,
        // });
        wx.setNavigationBarTitle({
          title: '民主评议',
        })
      },
    });
  },
  raduiOnChange(e) {
    const idx = e.currentTarget.dataset.idx;
    this.setData({
      [`voteData[${idx}]`]: {
        id: this.data.list[idx].id,
        key: e.detail.value,
      },
    }, () => {
      const D = this.data;
      const voteDataList = D.voteData || [];
      if (D.list.length === voteDataList.length) {
        this.setData({
          disabled: false,
        })
      }
    });
  },
  /**
   * 投票
   */
  onVote() {
    const D = this.data;
    const voteDataList = D.voteData || [];
    if (!voteDataList.length || D.list.length !== voteDataList.length) {
      wx.showModal({
        title: '提示',
        content: '请认真作答问卷',
        showCancel: false,
      });
      return;
    }
    let answer = '';
    voteDataList.forEach((item) => {
      const {
        id,
        key,
      } = item;
      answer += `${id}:${key},`;
    });
    $util.request({
      url: 'entry/wxapp/partylife.review.comfirm',
      data: {
        reviewid: D.id,
        answer: answer.slice(0, -1),
      },
      success: (res) => {
        wx.showToast({
          title: '评议成功',
          icon: 'success',
          duration: 1000,
          mask: true,
          success: () => {
            setTimeout(() => {
              wx.navigateBack();
            }, 1000);
          },
        });
      },
    });
  },
});
